package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.NoticeKindService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.NoticeKindMapper;
import cn.icare.cms.dao.po.MoneyOut;
import cn.icare.cms.dao.po.Notice;
import cn.icare.cms.dao.po.NoticeKind;
import cn.icare.cms.model.ro.AddNoticeKindRO;
import cn.icare.cms.model.ro.EditNoticeKindRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NoticeKindServiceImpl implements NoticeKindService {

    private final Logger logger = LoggerFactory.getLogger(NoticeKindServiceImpl.class);

    @Resource
    private NoticeKindMapper noticeKindMapper;

    @Override
    public Integer createOne(AddNoticeKindRO ro) {
        try {
            NoticeKind noticeKind = new NoticeKind();
            noticeKind.setName(ro.getName());
            int result = noticeKindMapper.insertSelective(noticeKind);
            if (result > 0) {
                return noticeKind.getId();
            }
        } catch (Exception e) {
            this.logger.error("createOne 异常", e);
        }

        return 0;
    }

    @Override
    public boolean updateOne(EditNoticeKindRO ro) {
        boolean isSuccess=false;
        try {
            NoticeKind noticeKind = new NoticeKind();
            noticeKind.setId(ro.getId());
            noticeKind.setName(ro.getName());
            int result = noticeKindMapper.updateByPrimaryKeySelective(noticeKind);
            if (result > 0) {
                isSuccess=true;
            }
        } catch (Exception e) {
            this.logger.error("createOne 异常", e);
        }

        return isSuccess;
    }

    @Override
    public boolean deleteOne(Integer id) {
        return this.noticeKindMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(NoticeKind.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.noticeKindMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);

        }
        return result;
    }

    @Override
    public NoticeKind getOne(Integer id) {
        return this.noticeKindMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(String name) {
        MsgDatasVO result = new MsgDatasVO();
        try {

            Example example = new Example(NoticeKind.class);
            Example.Criteria criteria = example.createCriteria();
            if(StringUtil.isNotEmpty(name))
            {
                criteria.andLike("name","%"+name+"%");
            }

            List<NoticeKind> dataList = this.noticeKindMapper.selectByExample(example);
            result.setTotal(dataList.size());
            result.setDatas(dataList);

        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
